package com.matt.service.impl;

import com.matt.domain.statistics.StatNurseryCourseCount;
import com.matt.foundation.utils.DateUtil;
import com.matt.repository.statistics.StatNurseryCourseCountRepository;
import com.matt.service.StatNurseryCourseCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.Objects;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/8/30
 * *******************************
 */
@Service
public class StatNurseryCourseCountServiceImpl implements StatNurseryCourseCountService {
    @Autowired
    private StatNurseryCourseCountRepository statNurseryCourseCountRepository;

    @Transactional
    @Override
    public Boolean updateTeacherClienteleCount(String areaId, Integer teacherCount, Integer clienteleCount, Date date) {
        String today = DateUtil.millisToDateTime(date.getTime(), "yyyy-MM-dd");
        Date begin = DateUtil.getCurrentDayBeginAndEnd(today).get("beg");
        Date end = DateUtil.getCurrentDayBeginAndEnd(today).get("end");
        StatNurseryCourseCount statNurseryCourseCount = statNurseryCourseCountRepository.findByAreaIdAndCreatedBetween(areaId, begin, end);
        if (Objects.nonNull(statNurseryCourseCount)){
            statNurseryCourseCount.setTeacherCount(teacherCount);
            statNurseryCourseCount.setClienteleCount(clienteleCount);
        }else {
            statNurseryCourseCount = new StatNurseryCourseCount();
            statNurseryCourseCount.setClienteleCount(clienteleCount);
            statNurseryCourseCount.setTeacherCount(teacherCount);
            statNurseryCourseCount.setAreaId(areaId);
            statNurseryCourseCount.setCreated(date);
        }
        statNurseryCourseCountRepository.save(statNurseryCourseCount);
        return true;
    }
}
